package cn.powerone.scm.service.impl;

import java.util.List;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.scm.domain.ScmInquiriesIn;
import cn.powerone.scm.domain.ScmProducts;
import cn.powerone.scm.service.IScmInquiriesInService;
import cn.powerone.scm.service.IScmProductsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmProjectProductsMapper;
import cn.powerone.scm.domain.ScmProjectProducts;
import cn.powerone.scm.service.IScmProjectProductsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目产品需求Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-06-27
 */
@Service
@RequiredArgsConstructor
public class ScmProjectProductsServiceImpl implements IScmProjectProductsService 
{
    private final ScmProjectProductsMapper scmProjectProductsMapper;

    private final IScmInquiriesInService inquiriesInService;

    private final IScmProductsService scmProductsService;

    private final IScmInquiriesInService scmInquiriesInService;

    /**
     * 查询项目产品需求
     * 
     * @param id 项目产品需求主键
     * @return 项目产品需求
     */
    @Override
    public ScmProjectProducts selectScmProjectProductsById(Long id)
    {
        return scmProjectProductsMapper.selectScmProjectProductsWithProductById(id);
//        return scmProjectProductsMapper.selectScmProjectProductsById(id);
    }

    /**
     * 查询项目产品需求列表
     * 
     * @param scmProjectProducts 项目产品需求
     * @return 项目产品需求
     */
    @Override
    public List<ScmProjectProducts> selectScmProjectProductsList(ScmProjectProducts scmProjectProducts)
    {
        return scmProjectProductsMapper.selectScmProjectProductsWithProductList(scmProjectProducts);
    }


    @Override
    public List<ScmProjectProducts> selectScmProjectProductsListByScmProjectProducts(ScmProjectProducts scmProjectProducts)
    {
        return scmProjectProductsMapper.selectScmProjectProductsList(scmProjectProducts);
    }


    /**
     * 新增项目产品需求
     * 
     * @param scmProjectProducts 项目产品需求
     * @return 结果
     */
    @Override
    public int insertScmProjectProducts(ScmProjectProducts scmProjectProducts)
    {
        //如果product id是空的，需要新增或者升级product
        if (scmProjectProducts.getProductId() == null){
            ScmProducts newProduct = scmProjectProducts.getScmProduct();
            newProduct.setDrawingNumber(scmProjectProducts.getDrawingNumber());
            newProduct.setVersion( scmProjectProducts.getVersion());
            newProduct.setInternalVersion(0L);//product servcie 还会自动生成
            newProduct.setProductName( scmProjectProducts.getProductName());
            newProduct.setProductStatus(1L);//来自前端,但是前端不允许修改。
            newProduct.setMaterial( scmProjectProducts.getMaterial());
            newProduct.setInflamingRetardingLevel( scmProjectProducts.getInflamingRetardingLevel());
            newProduct.setSpecification( scmProjectProducts.getSpecification());
            newProduct.setModel( scmProjectProducts.getModel());
            newProduct.setColor( scmProjectProducts.getColor());
            newProduct.setDrawingComposition( scmProjectProducts.getDrawingComposition());
            newProduct.setProductRemark( scmProjectProducts.getRemark());
//                newProduct.setDrawingAttachmentName();//来自前端
//                newProduct.setDrawingAttachmentUrl();//来自前端
            //如果products.id 也是空的，那就新增否则升级product
            if (newProduct.getId() != null){
                newProduct.setParentId(newProduct.getId());
                newProduct.setId(null);
            }
            scmProductsService.insertScmProducts(newProduct);
            scmProjectProducts.setProductId(newProduct.getId());
            scmProjectProducts.setInternalVersion(newProduct.getInternalVersion());
        }
        scmProjectProducts.setCreateTime(DateUtils.getNowDate());
        int row = scmProjectProductsMapper.insertScmProjectProducts(scmProjectProducts);
        //插入询价单
        if(row == 0)
            return 0;
        ScmInquiriesIn scmInquiriesIn = new ScmInquiriesIn();
        scmInquiriesIn.setProjectId(scmProjectProducts.getProjectId());
        scmInquiriesIn.setProductId(scmProjectProducts.getProductId());
        scmInquiriesIn.setProjectProductId(scmProjectProducts.getId());
        scmInquiriesIn.setDrawingNumber(scmProjectProducts.getDrawingNumber());
        scmInquiriesIn.setInternalVersion(scmProjectProducts.getInternalVersion());
        return scmInquiriesInService.insertScmInquiriesIn(scmInquiriesIn);
    }

    /**
     * 修改项目产品需求
     * 
     * @param scmProjectProducts 项目产品需求
     * @return 结果
     */
    @Override
    public int updateScmProjectProducts(ScmProjectProducts scmProjectProducts)
    {
        scmProjectProducts.setUpdateTime(DateUtils.getNowDate());
        return scmProjectProductsMapper.updateScmProjectProducts(scmProjectProducts);
    }

    /**
     * 批量删除项目产品需求
     * 
     * @param ids 需要删除的项目产品需求主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteScmProjectProductsByIds(Long[] ids)
    {
        try {
            inquiriesInService.deleteScmInquiriesInByProjectProductIds(ids);
            return scmProjectProductsMapper.deleteScmProjectProductsByIds(ids);
        }catch (org.springframework.dao.DataIntegrityViolationException e) {
            // 外键约束违反，无法删除有关联数据的记录
            System.err.println("删除失败：存在关联数据，无法删除");
            throw new RuntimeException("删除失败：存在关联数据，无法删除");
        } catch (Exception e) {
            System.err.println("删除过程中发生错误: " + e.getMessage());
            throw new RuntimeException("删除失败：" + e.getMessage());
        }
    }

    /**
     * 删除项目产品需求信息
     * 
     * @param id 项目产品需求主键
     * @return 结果
     */
    @Override
    public int deleteScmProjectProductsById(Long id)
    {
        try {
            inquiriesInService.deleteScmInquiriesInByProjectProductIds(new Long[]{id});
            return scmProjectProductsMapper.deleteScmProjectProductsById(id);
        }catch (org.springframework.dao.DataIntegrityViolationException e) {
            // 外键约束违反，无法删除有关联数据的记录
            System.err.println("删除失败：存在关联数据，无法删除");
            throw new RuntimeException("删除失败：存在关联数据，无法删除");
        } catch (Exception e) {
            System.err.println("删除过程中发生错误: " + e.getMessage());
            throw new RuntimeException("删除失败：" + e.getMessage());
        }
    }
}
